const {User, Role, Op} = require('../db/seq')

class UserServe {
    //获取用户信息
    async getUserInfo({id, username, password}) {
        const whereOpt = {}
        id && Object.assign(whereOpt, {id})
        username && Object.assign(whereOpt, {username})
        password && Object.assign(whereOpt, {password})
        const res = await User.findOne({
            attributes: ['id', 'username', 'password', 'status'],
            where: whereOpt
        })
        return res ? res.dataValues : null
    }

    // 注册用户
    async createUser(username, password, nickname) {
        const res = await User.create({username, password, nickname});
        return res.dataValues
    }

    async getUserListServe(ctx) {
        const {pageSize = 10, pageNum = 1, username, roleName, endTime, startTime} = ctx.request.query;
        const whereOptArr = []
        const whereOpt = {}
        username && Object.assign(whereOpt, {
            username: {
                // 模糊查询
                [Op.like]: '%' + username + '%'
            }
        });
        roleName && Object.assign(whereOpt, {
            roleName: {
                // 模糊查询
                [Op.like]: '%' + roleName + '%'
            }
        });
        startTime && endTime && Object.assign(whereOpt, {
            createdTime: {
                [Op.between]: [startTime, endTime]
            }
        });
        whereOptArr.push(whereOpt)
        const offset = (pageNum - 1) * pageSize;
        const {count, rows} = await User.findAndCountAll({
            attributes: {exclude: ['deletedTime', 'createdByUserId', 'password']},
            where: whereOptArr.length > 1 ? {
                [Op.and]: [
                    ...whereOptArr
                ]
            } : whereOpt,
            // include: {
            //     model: Role
            // },
            //分页
            offset,
            //限制
            limit: Number(pageSize)
        })
        return {
            total: rows.length,
            data: rows,
            pageNum,
            pageSize
        }
    }

    async getUserById(userId, needModel) {
        let res;
        if (needModel) {
            res = await User.findOne({
                where: {
                    id: userId
                },
                attributes: {
                    exclude: ['deletedTime', 'password', 'createdTime', 'updatedTime']
                },
                include: {
                    model: Role
                }
            })
        } else {
            res = await User.findOne({
                where: {
                    id: userId
                },
                attributes: {
                    exclude: ['deletedTime', 'password', 'createdTime', 'updatedTime', 'roleName']
                },
            })
        }

        return res
    }

    async getCurrentUserInfoServe(id) {
        const res = await User.findOne({
            where: {
                id
            },
            include: {
                model: Role,
                attributes: {
                    exclude: ['deletedTime'],
                }

            },
            attributes: {
                exclude: ['deletedTime', 'password', 'createdTime', 'updatedTime']
            },
        })
        return res
    }

    async updateUserRoleName(userId, roleName) {
        const res = await User.update({
            roleName
        }, {
            where: {
                id: userId
            }
        })
        return res[0] > 0;
    }

    async updateUserServe({username, password, nickname, id}) {
        const res = await User.update({
            username, password, nickname
        }, {
            where: {
                id
            }
        })
        return res[0] > 0;
    }

    async deleteUserServe(id) {
        const res = await User.destroy({
            where: {
                id
            }
        });
        return res > 0;
    }

    async updateUserStatusServe(id, status) {
        const res = await User.update({
            status
        }, {
            where: {
                id
            }
        })
        return res[0] > 0
    }

    async getUserStatusServe(id) {
        const res = await User.findOne({
            where: {
                id
            }
        })
        return res.dataValues ? res.dataValues.status : ''
    }
}


module.exports = new UserServe()
